package com.usefullc.bootstrap.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.entity.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author: Sampson
 * @date: 2022-06-21 16:03
 */
@RestController
@RequestMapping("/mock/minapp/")
@Api( tags="Mock")
public class MockController extends BaseController {

//    @Data
//    class MockResult{
//
//        private Integer errcode;
//
//        private String errmsg;
//
//    }

    @ApiOperation(value = "文件上传", response = WebResult.class)
    @PostMapping(path = "/wx/device/uploadVINFile2", headers = "content-type=multipart/form-data", produces = "application/json")
    public Object upload(@RequestParam MultipartFile file, @RequestParam String biz) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        jsonObject.put("errmsg","");
        jsonObject.put("vin", "12342314231");
        return jsonObject;
    }


    @RequestMapping(value={"/wx/user/login","/wx/user/appletCheck","/wx/user/phoneNumber",
            "/wx/permission/apply","/wx/permission/submit",
            "/wx/device/query","/wx/device/getDeviceCode","/wx/mine/historyDetail","/wx/device/uploadVINFile",
            "/wx/device/vin","/wx/mine/info","/wx/mine/historyRecord",
            },produces = "application/json")

    public Object tagContentList(HttpServletRequest request,@RequestParam(required = false) MultipartFile file){
        String path = request.getRequestURI();
        path = StringUtils.removeStart(path,"/mock/minapp");
        logger.info("path={}",path);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        jsonObject.put("errmsg","登录错误");
        if(StringUtils.equalsIgnoreCase(path,"/wx/user/login")){
//            jsonObject.put("errcode",2001);
            jsonObject.put("token","1111122222");

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/user/phoneNumber")){
            jsonObject.put("hasPermission",2000);

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/user/appletCheck")){
            jsonObject.put("status",1);

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/permission/apply")){  //进入权限申请页面获取手机号和所属公司列表
            jsonObject.put("phoneNumber","15267164682");
            String companyName = request.getParameter("companyName");
            int count  = 10;
            if(companyName.equalsIgnoreCase("杭州")){
                count = 7;
            }
            if(companyName.equalsIgnoreCase("长沙")){
                count = 5;
            }
            if(companyName.equalsIgnoreCase("北京")){
                count = 2;
            }
            if(companyName.equalsIgnoreCase("深圳")){
                count = 0;
            }
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < count; i++) {
                JSONObject jsonObjectItem = new JSONObject();
                jsonObjectItem.put("name",companyName+"xxxx有限公司"+i);
                jsonObjectItem.put("id",i);
                jsonArray.put(jsonObjectItem);
            }
            jsonObject.put("companyList",jsonArray);

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/permission/submit")){  //提交权限申请

            jsonObject.put("status",1);
            //errcode=0代表提交成功，1001代表已有权限，无须申请，

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/device/getDeviceCode")){  //历史记录页面 扫码带出deviceCode

            jsonObject.put("deviceCode","4321423141234123");
            //errcode=0代表提交成功，1001代表已有权限，无须申请，

        }else if(StringUtils.equalsIgnoreCase(path,"/wx/device/query") || StringUtils.equalsIgnoreCase(path,"/wx/mine/historyDetail")){
//            jsonObject.put("errcode",10);
            jsonObject.put("deviceStatus",1);
            jsonObject.put("canBind",1);
            jsonObject.put("optTime",1681732454);
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","deviceCode");
            jsonObjectItem.put("keyName","终端号");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","onlineStatus");
            jsonObjectItem.put("keyName","在线状态");
            jsonObjectItem.put("keyValue","在线");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","simCode");
            jsonObjectItem.put("keyName","SIM卡号");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","iccid");
            jsonObjectItem.put("keyName","ICCID");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","imei");
            jsonObjectItem.put("keyName","IMEI");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","lastLoginTime");
            jsonObjectItem.put("keyName","最后登录时间");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);


            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","softVersion");
            jsonObjectItem.put("keyName","软件版本");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);


            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","softVersionDate");
            jsonObjectItem.put("keyName","软件版本时间");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObjectItem = new JSONObject();
            jsonObjectItem.put("key","vin");
            jsonObjectItem.put("keyName","VIN");
            jsonObjectItem.put("keyValue","123456789098");
            jsonArray.put(jsonObjectItem);

            jsonObject.put("data",jsonArray);
        }else if(StringUtils.equalsIgnoreCase(path,"/wx/device/uploadVINFile")) {  //查询win
//            jsonObject.put("errcode",10);
            jsonObject.put("errmsg","");
            jsonObject.put("vin", "abc123456789qwert");
        }else if(StringUtils.equalsIgnoreCase(path,"/wx/device/vin")) {  //win 设置
            jsonObject.put("errcode",10);
            jsonObject.put("deviceStatus", 1);
            try {
                FileUtils.writeByteArrayToFile(new File("/Users/long3/aa.jpg"),file.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(StringUtils.equalsIgnoreCase(path,"/wx/mine/info")) {
            jsonObject.put("name", "张三");
            jsonObject.put("phoneNumber", "153***8765");
//                        jsonObject.put("hasPermission",10001001);  //未授权手机
            jsonObject.put("hasPermission",10002001);   // 未申请权限
//            jsonObject.put("hasPermission",10002002);   //权限审核中
//            jsonObject.put("hasPermission",10003001);   // 进入主页
        }else if(StringUtils.equalsIgnoreCase(path,"/wx/mine/historyRecord")) {  //设置历史
//            jsonObject.put("name", "张三");
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < 20; i++) {
                JSONObject jsonObjectItem = new JSONObject();
                jsonObjectItem.put("recordId",i+1);
                jsonObjectItem.put("deviceCode","1242342234233"+i);
                jsonObjectItem.put("deviceId","134214423423"+i);
                jsonObjectItem.put("deviceStatus", RandomUtils.nextInt(0,3));
                jsonArray.put(jsonObjectItem);
            }
            jsonObject.put("data",jsonArray);
        }
        return jsonObject;

    }










}
